package com.zhengshang.quartz.task;

import com.zhengshang.api.domain.AppVideo;
import com.zhengshang.api.domain.AppVideoDrama;
import com.zhengshang.api.mapper.AppVideoDramaMapper;
import com.zhengshang.api.mapper.AppVideoMapper;
import com.zhengshang.common.core.domain.entity.SysDictData;
import com.zhengshang.common.enums.UserStatus;
import com.zhengshang.common.oss.OssUtils;
import com.zhengshang.common.utils.DateUtils;
import com.zhengshang.common.utils.DictUtils;
import com.zhengshang.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("videoTask")
public class VideoTask
{
    protected final Logger logger = LoggerFactory.getLogger(VideoTask.class);
    @Autowired
    private AppVideoMapper appVideoMapper;
    @Autowired
    private AppVideoDramaMapper appVideoDramaMapper;

    /**
     * 自动更新免费视频
     */
    public void autoCopyVideo()
    {
        autoCopyVideo("video");
    }

    /**
     * 自动更新免费视频
     * @param videoType
     */
    public void autoCopyVideo(String videoType)
    {
        autoCopyVideo(videoType, DateUtils.datePath());
    }

    /**
     * 自动更新免费视频
     * @param videoType
     * @param path
     */
    public void autoCopyVideo(String videoType, String path)
    {
        List<SysDictData> dictDataList = DictUtils.getDictCache("app_video_type_oss");

        for (SysDictData sysDictData : dictDataList) {
            String keyPrefix = videoType + "/" + sysDictData.getDictLabel() + "/" + path + "/";
            List<String> objectList = OssUtils.listObjectV2(keyPrefix);
            for (int j = 0; j < objectList.size(); j++) {
                String oldKey = objectList.get(j);
                String objectID = OssUtils.copy(oldKey, videoType + "/" + path + "/" + OssUtils.newFileName(oldKey));
                if (StringUtils.isBlank(objectID)) {
                    continue;
                }
                String videoName = OssUtils.getVideoName(oldKey);
                AppVideo appVideo = new AppVideo();
                appVideo.setVideoClass("1");
                appVideo.setVideoName(videoName);
                appVideo.setVideoUrl(objectID);
                appVideo.setVideoType(sysDictData.getDictValue());
                appVideo.setVideoHeat("50");
                appVideo.setCreateBy("admin");
                appVideo.setCreateTime(DateUtils.getNowDate());
                appVideo.setDelFlag(UserStatus.OK.getCode());
                appVideo.setRemark(videoName);
                appVideoMapper.insertAppVideo(appVideo);
            }
            OssUtils.deleteFile(objectList);
        }
        logger.info("视频复制成功！");
    }

    /**
     * 自动更新电视剧
     */
    public void autoCopyTVPlay(){
        autoCopyTVPlay(DateUtils.dateTime());
    }

    /**
     * 自动更新电视剧
     * @param date
     */
    public void autoCopyTVPlay(String date){
        final String videoType = "tvplay";
        String path = DateUtils.datePath();
        String keyPrefix = videoType + "/" + date + "/";
        List<String> objectList = OssUtils.listObjectV2(keyPrefix);
        for (int j = 0; j < objectList.size(); j++) {
            String oldKey = objectList.get(j);
            String objectID = OssUtils.copy(oldKey, videoType + "/" + path + "/" + OssUtils.newFileName(oldKey));
            if (StringUtils.isBlank(objectID)) {
                continue;
            }
            String videoNames = OssUtils.getVideoName(oldKey);
            String[] names = videoNames.split("_");
            AppVideo appVideo;
            List<AppVideo> nameList = appVideoMapper.selectAppVideoListByName(names[0]);
            if (nameList == null || nameList.size() == 0) {
                appVideo = new AppVideo();
                appVideo.setVideoClass("1");
                appVideo.setVideoName(names[0]);
                appVideo.setVideoType("1");
                appVideo.setVideoHeat("50");
                appVideo.setCreateBy("admin");
                appVideo.setCreateTime(DateUtils.getNowDate());
                appVideo.setDelFlag(UserStatus.OK.getCode());
                appVideo.setRemark(names[0]);
                appVideoMapper.insertAppVideo(appVideo);
            } else {
                appVideo = nameList.get(0);
            }

            AppVideoDrama appVideoDrama = new AppVideoDrama();
            appVideoDrama.setVideoId(appVideo.getId());
            appVideoDrama.setDramaUrl(objectID);
            appVideoDrama.setDrama(Integer.parseInt(names[1]));
            appVideoDrama.setCreateBy("admin");
            appVideoDrama.setCreateTime(DateUtils.getNowDate());
            appVideoDramaMapper.insertAppVideoDrama(appVideoDrama);
        }
        OssUtils.deleteFile(objectList);
    }

//    /**
//     * 自动更新在线教育视频
//     * @param videoType
//     * @param path
//     */
//    public void autoCopyEducation(String videoType, String path)
//    {
//        List<SysDictData> dictDataList = DictUtils.getDictCache("app_video_type_oss");
//
//        for (SysDictData sysDictData : dictDataList) {
//            String keyPrefix = videoType + "/" + sysDictData.getDictLabel() + "/" + path + "/";
//            List<String> objectList = OssUtils.listObjectV2(keyPrefix);
//            for (int j = 0; j < objectList.size(); j++) {
//                String oldKey = objectList.get(j);
//                String objectID = OssUtils.copy(oldKey, videoType + "/" + path + "/" + OssUtils.newFileName(oldKey));
//                if (StringUtils.isBlank(objectID)) {
//                    continue;
//                }
//                String videoName = OssUtils.getVideoName(oldKey);
//                AppVideo appVideo = new AppVideo();
//                appVideo.setVideoClass("2");
//                appVideo.setVideoName(videoName);
//                appVideo.setVideoUrl(objectID);
//                appVideo.setVideoType(sysDictData.getDictValue());
//                appVideo.setVideoHeat("50");
//                appVideo.setCreateBy("admin");
//                appVideo.setCreateTime(DateUtils.getNowDate());
//                appVideo.setDelFlag(UserStatus.OK.getCode());
//                appVideo.setRemark(videoName);
//                appVideoMapper.insertAppVideo(appVideo);
//            }
//            OssUtils.deleteFile(objectList);
//        }
//        logger.info("视频复制成功！");
//    }
}
